// IntegralMall.js
// var categoryData = require("../data/category")
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchKeyword: '', //需要搜索的字符 
        searchSongList: [], //放置返回数据的数组 
        isFromSearch: true, // 用于判断searchSongList数组是不是空数组，默认true，空的数组 
        pageIndex: 1, // 设置加载的第几次，默认是第一次 
        callbackcount: 5, //返回数据的个数 
        searchLoading: false, //"上拉加载"的变量，默认false，隐藏 
        searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏 
        currentIndex: 0

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var res = wx.getSystemInfoSync();
        let height = res.screenHeight;
        this.setData({
            height: height
        })
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/list.do',
            data: {
                categoryId: 1
            },
            success: function(res) {
                console.log(res)
                that.setData({
                    categoryList: res.data.categoryList,
                    categoryId: res.data.categoryList[0].id,
                    goodsList: res.data.goodsList,
                    hotList: res.data.hotList
                })
            }
        })
    },
    onAside: function(e) {
        //下标
        var that = this;
        var index = e.currentTarget.dataset.index;
        var categoryId = e.currentTarget.id;
        //点击分类时重新请求加载数据
        that.setData({
            goodsList: [],
            hotList: []
        })


        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/categoryList.do',
            data: {
                categoryId: categoryId
            },
            success: function(res) {
                that.setData({
                    categoryList: res.data.categoryList,
                    categoryId: e.currentTarget.id,
                    goodsList: res.data.goodsList,
                    hotList: res.data.hotList
                })
            }
        })
        that.setData({
            currentIndex: index,
            categoryId: categoryId,
            pageIndex: 1,

        });

    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh()
    }, // 上拉加载回调接口
    scrolltolower: function() {
        var that = this
        var pageIndex = that.data.pageIndex;
        var categoryId = that.data.categoryId;
        wx.request({
            url: app.data.baseUrl + '/AibraUsb/goods/pageList.do',
            data: {
                categoryId: that.data.categoryId,
                pageIndex: that.data.pageIndex
            },
            success: function(res) {
                var goodsList1 = res.data.goodsList;
                var goodsList2 = that.data.goodsList;
                goodsList2 = goodsList2.concat(goodsList1)
                pageIndex = ++pageIndex;
                that.setData({
                    goodsList: goodsList2,
                    pageIndex: pageIndex
                })
            },
            fail: function() {
                console.log("失败")
            }
        })
        that.data.searchPageNum++;
        // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    bindproductInfo: function(e) {
        var goodsId = e.currentTarget.id
        wx.navigateTo({
            url: '../productDetail/productDetail?goodsId=' + goodsId,
        })
    }

})